/**
 * 
 */
package com.gs.practice.thread.javathreads.ch02.ex7;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.gs.practice.thread.javathreads.ch02.CharacterDisplayCanvas;
import com.gs.practice.thread.javathreads.ch02.CharacterEventHandler;
import com.gs.practice.thread.javathreads.ch02.CharacterListener;
import com.gs.practice.thread.javathreads.ch02.CharacterSource;

/**
 * @author Sabuj Das | sabuj.das@gmail.com
 *
 */
public class SwingTypeTester extends JFrame implements CharacterSource {

	
	private RandomCharacterGenerator producer;
	private AnimatedCharacterDisplayCanvas displayCanvas;
	private CharacterDisplayCanvas feedBackCanvas;
	private JButton startButton, quitButton;
	private CharacterEventHandler handler;
	
	/**
	 * 
	 */
	public SwingTypeTester() {
		initComponents();
		
	}
	
	/**
	 * 
	 */
	private void initComponents() {
		setLocation(100, 100);
		setTitle("Character Type Test");
		setResizable(false);
		getContentPane().setLayout(new BorderLayout());
		
		handler = new CharacterEventHandler();
		displayCanvas = new AnimatedCharacterDisplayCanvas();
		feedBackCanvas = new CharacterDisplayCanvas(this);
		startButton = new JButton("Start");
		quitButton = new JButton("Quit");
		
		getContentPane().add(displayCanvas, BorderLayout.NORTH);
		getContentPane().add(feedBackCanvas, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.add(startButton);
		panel.add(quitButton);
		
		getContentPane().add(panel, BorderLayout.SOUTH);
		
		addWindowListener(new WindowAdapter() {
			
			@Override
			public void windowClosing(WindowEvent e) {
				quit();
			}
			
		});
		
		feedBackCanvas.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent keyEvent) {
				char ch = keyEvent.getKeyChar();
				if(ch != KeyEvent.CHAR_UNDEFINED){
					newCharacter((int)ch);
				}
			}

		});
		
		startButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				producer = new RandomCharacterGenerator();
				displayCanvas.setCharacterSource(producer);
				displayCanvas.setDone(false);
				new Thread(displayCanvas).start();
				startButton.setEnabled(false);
				feedBackCanvas.setEnabled(true);
				feedBackCanvas.requestFocus();
			}
		});
		
		quitButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				displayCanvas.setDone(true);
				//quit();
			}
		});
		
		pack();
	}
	
	private void quit(){
		System.exit(0);
	}
	
	private void newCharacter(int ch) {
		handler.fireNewCharacter(this, ch);
	}

	@Override
	public void addCharacterListener(CharacterListener listener) {
		handler.addCharacterListener(listener);
	}

	@Override
	public void removeCharacterListener(CharacterListener listener) {
		handler.removeCharacterListener(listener);
	}

	@Override
	public void nextCharacter() {
		throw new IllegalStateException("We don't produce on-demand");
	}

	
	public static void main(String[] args) {
		new SwingTypeTester().setVisible(true);
	}
	
}
